package jfood.waiter;

import jade.content.lang.Codec;
import jade.content.lang.sl.SLCodec;
import jade.content.onto.Ontology;
import jade.core.Agent;
import jade.domain.DFService;
import jade.domain.FIPAException;
import jade.domain.FIPANames;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.Property;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import jfood.gui.WaiterGui;
import jfood.ontology.meal.MealOntology;
import jfood.ontology.query.QueryOntology;

public class WaiterAgent extends Agent {

	protected Codec codec = new SLCodec();
	protected Ontology queryOntology = QueryOntology.getInstance();
	protected Ontology mealOntology = MealOntology.getInstance();
	protected DFAgentDescription dfd;
	protected WaiterGui gui;
	private static final String SERVICE_NAME = "Waiter";
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -6495310573869128003L;

	protected void setup() {
		getContentManager().registerLanguage(codec);
		getContentManager().registerOntology(queryOntology, QueryOntology.NAME);
		getContentManager().registerOntology(mealOntology, MealOntology.NAME);
		registerService();
	}
	
	private void registerService() {
		dfd = new DFAgentDescription();
		dfd.setName(getAID());
		ServiceDescription sd = new ServiceDescription();
		sd.setName(SERVICE_NAME );
		sd.setType("meal-serving");
		sd.addLanguages(FIPANames.ContentLanguage.FIPA_SL);
		sd.addProperties(new Property("country", "Poland"));
		dfd.addServices(sd);
		sd.addOntologies(MealOntology.NAME);
		sd.addOntologies(QueryOntology.NAME);		
		regService();
	}
	
	public void regService() {
		try {
			DFService.register(this, dfd);
		}
		catch (FIPAException e) {
			System.err.println(getLocalName() +
					" registration with DF unsucceeded. Reason: " + e.getMessage());
			doDelete();
		}
		addBehaviour(new WaitForCustomerBehaviour());
	}
	
	public void unregService() {
		try {
			DFService.deregister(this, dfd);
		}
		catch (FIPAException e) {
			System.err.println(getLocalName() +
					" registration with DF unsucceeded. Reason: " + e.getMessage());
			doDelete();
		}
	}
	
	public void askForMeal(String message) {
		this.addBehaviour(new AskForMealBehaviour(message));
	}
	
}
